In [1]:
from PIL import Image
import numpy as np
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('bmh')
matplotlib.rcParams['figure.figsize']=(8,5)
簡易的 linear regression 實驗
In [2]:
# 產生隨機數據
X = np.random.normal(0, 3, size=(50,1))
Y = X @ [3] + np.random.normal(0, size=50)
# 畫出來看看
plt.plot(X, Y, 'o');
In [3]:
# 用 numpy 的 lstsq
a = np.linalg.lstsq(X, Y)[0]
a
Out[3]:
In [4]:
# 畫出來
plt.plot(X, Y, 'o')
plt.plot(X, X @ a, 'o');
In [5]:
%run -i q_lstsq.py
In [6]:
from sklearn import linear_model
In [7]:
X = np.random.normal(0, 3, size=(50,1))
Y = X @ [3] + 4 +np.random.normal(0, size=50)
In [8]:
regr = linear_model.LinearRegression()
regr
Out[8]:
In [9]:
regr.fit(X,Y)
print(regr.coef_, regr.intercept_)
In [10]:
# 畫出來
plt.plot(X, Y, 'o')
plt.plot(X, regr.predict(X), 'o');
In [11]:
%run -i q_linear_test.py
In [12]:
from sklearn import datasets
In [13]:
diabetes = datasets.load_diabetes()
diabetes
Out[13]:
In [14]:
import scipy.stats
In [15]:
scipy.stats.describe(diabetes.target)
Out[15]:
In [16]:
idx = np.arange(diabetes.data.shape[0])
np.random.shuffle(idx)
X = diabetes.data[idx]
y = diabetes.target[idx]
試試看 linear regression
In [17]:
train_X = X[:-50, 2:3]
train_y = y[:-50]
test_X = X[-50:, 2:3]
test_y = y[-50:]
regr = linear_model.LinearRegression()
regr.fit(train_X, train_y)
plt.plot(train_X, train_y, 'o');
plt.plot(train_X, regr.predict(train_X), 'o');
np.mean((regr.predict(train_X)-train_y)**2)
Out[17]:
In [18]:
plt.plot(test_X, test_y, 'o');
plt.plot(test_X, regr.predict(test_X), 'o');
In [19]:
train_X = X[:-50]
train_y = y[:-50]
test_X = X[-50:]
test_y = y[-50:]
regr = linear_model.LinearRegression()
regr.fit(train_X, train_y)
np.mean((regr.predict(train_X)-train_y)**2)
Out[19]:
In [20]:
np.mean((regr.predict(test_X)-test_y)**2)
Out[20]:
In [21]:
plt.plot(test_X[:, 2:3], test_y, 'o');
plt.plot(test_X[:, 2:3], regr.predict(test_X), 'o');
In [22]:
plt.scatter(regr.predict(train_X), train_y, c='g', s=3)
plt.scatter(regr.predict(test_X), test_y, c='b')
plt.plot([0,300],[0,300],'r', linewidth=1);
In [23]:
groups = np.arange(30,300,60)
predict_y=regr.predict(train_X)
plt.boxplot([train_y[(predict_y>=i-30)&(predict_y< i+30)] for i in groups], labels=groups);
plt.plot(np.arange(1,len(groups)+1), groups,'x');
In [24]:
regr = linear_model.Lasso(alpha=0.001)
regr.fit(train_X, train_y)
np.mean((regr.predict(train_X)-train_y)**2)
Out[24]:
In [25]:
np.mean((regr.predict(test_X)-test_y)**2)
Out[25]:
In [26]:
from sklearn import model_selection
α_space = np.logspace(-4, 0, 50)
scores =[]
for α in α_space:
regr.alpha = α
s = model_selection.cross_val_score(regr, train_X, train_y, cv=3)
scores.append((s.mean(), s.std()))
scores=np.array(scores).T
plt.semilogx(α_space, scores[0], 'r')
plt.semilogx(α_space, scores[0]+scores[1],'b--')
plt.semilogx(α_space, scores[0]-scores[1],'b--')
plt.fill_between(α_space, scores[0] + scores[1], scores[0] - scores[1], alpha=0.2);
In [27]:
regr = linear_model.LassoCV(alphas = α_space)
regr.fit(train_X, train_y)
print(regr.alpha_)
np.mean((regr.predict(train_X)-train_y)**2)
Out[27]:
In [28]:
np.mean((regr.predict(test_X)-test_y)**2)
Out[28]:
Linear regression 不是個好方法
In [29]:
X = np.random.normal(1, size=(100,1))
y = (X[:,0]>0).ravel()*2-1
regr = linear_model.LinearRegression().fit(X, y)
test_X=np.linspace(-3,3,10).reshape(-1,1)
plt.plot(X, y, 'x');
plt.plot(test_X, regr.predict(test_X), 'r')
plt.plot([-regr.intercept_/regr.coef_[0]]*2, [-1.5,1.5], 'r--')
regr.intercept_
Out[29]:
In [30]:
regr.intercept_
Out[30]:
In [31]:
import gzip
import pickle
with gzip.open('mnist.pkl.gz', 'rb') as f:
train_set, validation_set, test_set = pickle.load(f, encoding='latin1')
train_X, train_y = train_set
test_X, test_y = test_set
In [32]:
regr.fit(train_X, train_y)
regr.predict(test_X)
Out[32]:
In [33]:
predict_y = np.floor(regr.predict(train_X)+0.5).astype('int').clip(0,9)
np.mean(predict_y == train_y)
Out[33]:
In [34]:
predict_y = np.floor(regr.predict(test_X)+0.5).astype('int').clip(0,9)
np.mean(predict_y == test_y)
Out[34]:
準確率約 23% 很低
In [35]:
train_Y = np.zeros(shape=(train_y.shape[0], 10))
train_Y[np.arange(train_y.shape[0]), train_y] = 1
In [36]:
train_y[0]
Out[36]:
In [37]:
train_Y[0]
Out[37]:
In [38]:
from sklearn.preprocessing import OneHotEncoder
onehot_encoder = OneHotEncoder()
onehot_encoder.fit(train_y.reshape(-1,1))
onehot_encoder.transform(train_y.reshape(-1,1)).toarray()[0]
Out[38]:
In [39]:
# 訓練模型
regr.fit(train_X, train_Y)
# 用 argmax 得到結果
predict_y = np.argmax(regr.predict(train_X), axis=1)
# 計算正確率
np.mean(predict_y == train_y)
Out[39]:
In [40]:
%run -i q_minst_linear_regression.py